//
//  LLLookViewController.m
//  ListenAndLook
//
//  Created by zhangtao on 15/10/21.
//  Copyright © 2015年 zt.td. All rights reserved.
//

#import "LLLookViewController.h"

@interface LLLookViewController ()
@property (strong, nonatomic) IBOutlet UISlider *mySliter;

@property (strong, nonatomic) IBOutlet UIProgressView *progress;

@property (weak, nonatomic) IBOutlet UIButton *playBtn;


@property (weak, nonatomic) IBOutlet UIImageView *imageIV;

@end

@implementation LLLookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    _progress = _lll.progress;
//    _timer = _lll.timer;
//    if (!_mySliter) {
//    
//    if (self.passText.length <1) {
    if(self.coverImg.length<5){
        self.imageIV.hidden = NO;
        [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, [UIFont systemFontOfSize:20.0],NSFontAttributeName,nil]];
    UIImage *image =[UIImage imageNamed:@"4.png"];
    self.imageIV.image = image;
    }else{
//        self.imageIV.hidden = YES;

//        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//        paragraphStyle.lineSpacing = 20;// 字体的行间距
//        
//        NSDictionary *attributes = @{
//                                     NSFontAttributeName:[UIFont systemFontOfSize:15],
//                                     NSParagraphStyleAttributeName:paragraphStyle
//                                     };
//        self.text.attributedText = [[NSAttributedString alloc] initWithString:self.passText attributes:attributes];

        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_coverImg]]];
        self.imageIV.image = image;

    }


    [_mySliter addTarget:self action:@selector(sliterChange:) forControlEvents:UIControlEventTouchUpInside];
    
        _mySliter.minimumTrackTintColor = [UIColor greenColor];
        _mySliter.maximumTrackTintColor = [UIColor grayColor];
        _mySliter.value = 0;
        [_mySliter setThumbImage:[UIImage imageNamed:@"detail_icon_schedule_ball_green.png"] forState:UIControlStateNormal];
    [_mySliter setMaximumTrackImage:[UIImage imageNamed:@"left1"] forState:UIControlStateNormal];
//    [_mySliter setBackgroundColor:RGB(73, 87, 88, 1)];
    [_mySliter setMinimumTrackImage:[UIImage imageNamed:@"圆角矩形-1-拷贝-2"] forState:UIControlStateNormal];
    [_mySliter setThumbImage:[UIImage imageNamed:@"detail_icon_schedule_ball_green.png"] forState:UIControlStateNormal];                    //detail_icon_schedule_ball_green.png
    
//    [_mySliter addTarget:self action:@selector(sliterChange:) forControlEvents:UIControlEventTouchUpInside];
    
    
        _mySliter.continuous = YES;
//    }
    
    UINavigationBar *nav = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 70)];
   
    [nav setBackgroundImage:[UIImage imageNamed:@"NavBack.png"] forBarMetrics:UIBarMetricsDefault];
    
    UINavigationItem *navigationItem = [[UINavigationItem alloc]initWithTitle:@"课文边听边看"];

    
    
    [nav setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor whiteColor], UITextAttributeTextColor,
                                 
                                                                     nil]];
    
//     UIImage *image = [[UIImage imageNamed:@"返回箭头"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem  *leftBtn = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"返回箭头"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ] style:UIBarButtonItemStylePlain target:self action:@selector(leftBtnAction)];
    navigationItem.leftBarButtonItem = leftBtn;
    
    [nav pushNavigationItem:navigationItem animated:NO];

    [self.view addSubview:nav];
    [self updateProgress];
   
}

-(void)sliterChange:(UISlider *)slider{
    [self.lll.player seekToTime:CMTimeMakeWithSeconds(slider.value, 1)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)leftBtnAction{
    [self dismissViewControllerAnimated:YES completion:nil];
    _mySliter = nil;
}

- (IBAction)playAndPause:(id)sender {
//    if (![_lll.audioPlayer isPlaying]) {
//        [_lll.audioPlayer play];
//        _lll.timer.fireDate=[NSDate distantPast];//恢复定时器
//    }else if ([_lll.audioPlayer isPlaying]) {
//        [_lll.audioPlayer pause];
//        _lll.timer.fireDate=[NSDate distantFuture];//暂停定时器，注意不能调用invalidate方法，此方法会取消，之后无法恢复
//    }
    
    
//    AVPlayer
    if(_lll.player.rate==0){ //说明时暂停
         [_lll.player play];
               [sender setImage:[UIImage imageNamed:@"music_detail_stop.png"] forState:UIControlStateNormal];
    }else if(_lll.player.rate==1){//正在播放
        [_lll.player pause];
               [sender setImage:[UIImage imageNamed:@"music_detail_start.png"] forState:UIControlStateNormal];
    }

//    
       
}


-(void)updateProgress{
  

    [_lll.player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
        float current=CMTimeGetSeconds(time);
        float total=CMTimeGetSeconds([_lll.playerItem duration]);
        if (current) {
            
            [_mySliter setValue:current animated:YES];
            _mySliter.maximumValue = total;
           
        }

    }];
}




@end
